//
//  YCAppDelegate.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/26.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCAppDelegate.h"
#import "YCNavigationViewController.h"
#import "YCHomeViewController.h"
#import "YCLoginViewController.h"
#import "BNCoreServices.h"
#import <JSPatch/JPEngine.h>

///密钥
NSString * const YCJSPatchEncryptKey = @"2871714D2B554D6B";//(qqM+UMK
///向量
NSString * const YCJSPatchEncryptIV  = @"484A374B5B674E48";//HJ7k[gNH


@interface YCAppDelegate ()

/** 地图管理者  */
@property (nonatomic, strong) BMKMapManager *mapManager;

@end

@implementation YCAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self runPatch];//运行补丁
    
    //集成百度地图
    // 要使用百度地图，请先启动BaiduMapManager
    self.mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:BaiDuAppKey  generalDelegate:nil];
    if (!ret) {
        YCLog(@"manager start failed!");
    }
    
    // 初始化导航SDK  //BaiduNavi_iOSSDK_v2.0.6版本
    [BNCoreServices_Instance initServices:BaiDuAppKey];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 主界面
//    UIViewController *rootVc = [[YCHomeViewController alloc] init];
    
    // 登录注册
    UIViewController *rootVc = [[YCLoginViewController alloc] init];
    
    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
    self.window.rootViewController = nav;
    
    
    [self.window makeKeyAndVisible];
    
    [self configStyle];
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    
}


#pragma mark - 运行补丁
/// 补丁
- (void)runPatch
{
    NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"release.1.1.40" ofType:@"js"];
    NSString *js = [[NSString alloc] initWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];
    if(js)
    {
        [JPEngine startEngine];
        [JPEngine evaluateScript:js];
    }
    return;
    
    NSString *config = @"";
#if DEBUG
    config = @"debug";
#else
    config = @"release";
#endif
    
    NSInteger v = [[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey] integerValue];//bundle内部版本号
    NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];//发布版本号,3位如:1.1.0
    NSString *fileName = [NSString stringWithFormat:@"patch.%@.%ld.luac",version,(long)v];
    
    NSString *filepath = [[UIApplication sharedApplication].yc_documentsPath stringByAppendingPathComponent:fileName];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://app.gmall.com/myapp/%@/%@",config,fileName]];
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:3];
    NSError *error;
    NSHTTPURLResponse *response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    if(!error && response.statusCode == 200)
    {
        [data writeToFile:filepath atomically:YES];
    }
    else
    {
        data = [NSData dataWithContentsOfFile:filepath];
    }
    
    if(data)
    {
        data = [data yc_decryptWithKey:[YCJSPatchEncryptKey yc_stringOfHexString]  iv:[YCJSPatchEncryptIV yc_stringOfHexString]];
        if(!error)
        {
            NSString *js = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            if(js)
            {
                [JPEngine startEngine];
                [JPEngine evaluateScript:js];
            }
        }
    }
}

- (void)configStyle
{
    // 配置提示
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];

}


@end
